package cn.itcast.service.impl;

import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;


/**
 * @author Administrator
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao ;
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }


    /**
     *分页条件查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }


    public void edit(Role role,Integer[] permissionIds,Integer[] menuIds) {
         //修改角色
        roleDao.update(role);
        //删除角色关联权限
        roleDao.deletePermissionAssociation(role.getId());
        setRoleAndPermission(role.getId(),permissionIds);
        roleDao.deleteMenuAssociation(role.getId());
        setRoleAndMenus(role.getId(),menuIds);

    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //添加角色
       roleDao.add(role);
       //关联添加角色和权限
       if (permissionIds!=null && permissionIds.length > 0){
           Integer roleId = role.getId();
           setRoleAndPermission(roleId,permissionIds);
       }
       //关联添加角色和菜单
       if (menuIds!=null && menuIds.length > 0){
           Integer roleId = role.getId();
           setRoleAndMenus(roleId,menuIds);
       }
    }

    /**
     * 通过角色查询权限列表
     * @param roleId
     * @return
     */
    public Integer[] selectedPermissionsId(Integer roleId){

        return roleDao.findPermissionsAssociationIds(roleId);
    }
    /**
     * 通过角色查询菜单列表
     *
     */
    public Integer[] selectedMenusId(Integer roleId){
        return roleDao.findMenusAssociationIds(roleId);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //删除关联权限信息
        roleDao.deletePermissionAssociation(id);
        roleDao.deleteMenuAssociation(id);
        roleDao.deleteById(id);
    }
//      关联角色和菜单信息
    private void setRoleAndMenus(Integer roleId, Integer[] menuIds) {
        Stream.of(menuIds).forEach(menuId-> roleDao.setRolesAndMenusAssociation(roleId,menuId));
    }
//         关联角色和权限信息
    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        Stream.of(permissionIds).forEach(permissionId-> roleDao.setRolesAndPermissionAssociation(roleId,permissionId));

    }
}
